package com.avic.rong;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import com.avic.rong.bean.show_bean.ChannelBean;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * created by xtj on 2018/9/11 16:19.
 */
public abstract class BaseFragmentViewPagerAdapter extends FragmentPagerAdapter {

    protected List<ChannelBean> channelList=new ArrayList<>();
    private HashMap<String, Fragment> fragments = new HashMap<String, Fragment>();
    private FragmentManager fm;
    public BaseFragmentViewPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fm = fm;
    }


    @Override
    public int getCount() {
        return channelList.size();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
//            fragments.remove(Integer.valueOf(position));
    }

    @Override
    public Fragment getItem(int position) {
        return getFragment(position);
    }

    public Fragment getFragment(int position) {
        if (null == fragments.get(channelList.get(position).getId())) {
//            LogApp.i("TAG", "fragment" + position + "为空");
            Fragment fragment = getTabFragment(channelList.get(position).getId(),position);
            fragments.put(channelList.get(position).getId(),fragment);
            return fragment;
        } else {
//            LogApp.i("TAG", "fragment" + position + "不为空");
            return fragments.get(channelList.get(position).getId());
        }

    }
    public abstract Fragment getTabFragment(String tab, int position);
    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    public void setFragments(List<ChannelBean> list) {
        this.channelList.clear();
        this.channelList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        Object obj = super.instantiateItem(container, position);
        return obj;
    }

//    @Override
//    public Parcelable saveState() {
//        return null;
//    }

    @Override
    public CharSequence getPageTitle(int position) {
        return channelList.get(position).getName();
    }
}
